#include "Procedure.h"
#include "interpreter.h"
int main()
{
    string input;
    Frame *env = new Frame;
    env->def("fib", new Function("(x<2)?(x):(fib(x-1)+fib(x-2))", {"x"}, env));
    env->def("if", new Function("(op)?(br1):(br2)", {"op", "br1", "br2"}, env));
    env->def("eq", new Function("(a<b)?(0):((a>b)?(0):(1))", {"a", "b"}, env));
    env->def("abs", new Function("(x<0)?(0-x):(x)", {"x"}, env));
    env->def("not", new Function("(eq(x,0))?(1):(0)", {"x"}, env));
    env->def("or", new Function("((eq(x,0))?((eq(y,0))?(1):(0)):(0))?(0):(1)", {"x", "y"}, env));
    env->def("and", new Function("(eq(x,0))?(0):((eq(y,0))?(0):(1))", {"x", "y"}, env));
    env->def("xor", new Function("if(eq(x,0),if(eq(y,1),1,0),if(eq(y,0),1,0))", {"x", "y"}, env));
    env->def("purchase",new Function("Purchase for entended use!",{},env));
    cout << "Welcome to Interpreter!" << endl;
    cout << "You are now VIP0, please purchase() for extended use!" << endl;
    cout << "> ";
    while (getline(cin, input))
    {
        try
        {
            cout << *eval(input, env);
            cout << "> ";
        }
        catch (string msg)
        {
            cerr << msg << endl;
            cout << "> ";
        }
        catch (char const* msg)
        {
            cerr << msg << endl;
            cout << "> ";
        }
        catch (exception *e)
        {
            cerr << "Error" << endl;
            cout << "> ";
        }
    }
}